package com.example.user.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.context.UserContext;
import com.example.common.util.AdminUtil;
import com.example.common.response.PageResponse;
import com.example.user.entity.Address;
import com.example.user.entity.AddressExample;
import com.example.user.mapper.AddressMapper;
import com.example.user.request.AddressListRequest;
import com.example.user.response.AddressResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.util.SnowflakeUtil;
import com.example.user.request.AddressRequest;

@Service
public class AddressService {
    private static final Logger log = LoggerFactory.getLogger(AddressService.class);
    @Autowired
    AddressMapper addressMapper;

    @Autowired
    AdminUtil adminUtil;

    public PageResponse<AddressResponse> list(AddressListRequest request) {
        AddressExample addressExample = new AddressExample();
        // 按 id 字段逆序排序
        addressExample.setOrderByClause("id desc");

        // 查询条件
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (!adminUtil.isAdmin(UserContext.getUserId())) {
            // 增加查询条件：按「当前登录用户 id 字段」进行过滤
            criteria.andUserIdEqualTo(UserContext.getUserId());
        }

        // 增加查询条件：关键词搜索
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            String keyword = "%" + request.getKeyword() + "%";
            criteria.andFullAddressLike(keyword);
        }

        PageHelper.startPage(request.getPage(), request.getSize());
        log.info("当前页码 current_page = {}", request.getPage());
        log.info("每页记录数 per_page = {}", request.getSize());

        List<Address> addresss = addressMapper.selectByExample(addressExample);

        PageInfo<Address> pageInfo = new PageInfo<>(addresss);
        log.info("总记录数 total = {}", pageInfo.getTotal());
        log.info("总页数 pages = {}", pageInfo.getPages());

        List<AddressResponse> list = BeanUtil.copyToList(addresss, AddressResponse.class);

        PageResponse<AddressResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);

        return pageResponse;
    }
    public void save(AddressRequest request) {
        Address address = BeanUtil.copyProperties(request, Address.class);
        DateTime now = DateTime.now();
        if (ObjectUtil.isNull(address.getId()) || address.getId() == 0L) {
            address.setId(SnowflakeUtil.getId());
            address.setCreatedAt(now);
            address.setUpdatedAt(now);
            address.setUserId(UserContext.getUserId());
            addressMapper.insert(address);
        } else {
            address.setUpdatedAt(now);
            addressMapper.updateByPrimaryKeySelective(address);
        }
    }

    public int deleteBatch(List<Long> ids) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdIn(ids);
        return addressMapper.deleteByExample(addressExample);
    }
}
